home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / libs / libelf-0.5 / libelf-0 / libelf-0.5.2 / getarsym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-19  |  2.2 KB  |  84 lines

  1. /*
  2. getarsym.c - implementation of the elf_getarsym(3) function.
  3. Copyright (C) 1995 Michael Riepe <riepe@ifwsn4.ifw.uni-hannover.de>
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19.  
  20. #include <private.h>
  21. #include <byteswap.h>
  22.  
  23. Elf_Arsym*
  24. elf_getarsym(Elf *elf, size_t *ptr) {
  25.     Elf_Arsym *syms;
  26.     size_t count;
  27.     size_t tmp;
  28.     size_t i;
  29.     char *s;
  30.     char *e;
  31.  
  32.     if (!ptr) {
  33.     ptr = &tmp;
  34.     }
  35.     *ptr = 0;
  36.     if (!elf) {
  37.     return NULL;
  38.     }
  39.     elf_assert(elf->e_magic == ELF_MAGIC);
  40.     if (elf->e_kind != ELF_K_AR) {
  41.     seterr(ERROR_NOTARCHIVE);
  42.     return NULL;
  43.     }
  44.     if (elf->e_symtab && !elf->e_free_syms) {
  45.     if (elf->e_symlen < 4) {
  46.         seterr(ERROR_SIZE_ARSYMTAB);
  47.         return NULL;
  48.     }
  49.     count = __load_u32M(elf->e_symtab);
  50.     if (elf->e_symlen < 4 * (count + 1)) {
  51.         seterr(ERROR_SIZE_ARSYMTAB);
  52.         return NULL;
  53.     }
  54.     if (!(syms = (Elf_Arsym*)malloc((count + 1) * sizeof(*syms)))) {
  55.         seterr(ERROR_MEM_ARSYMTAB);
  56.         return NULL;
  57.     }
  58.     s = elf->e_symtab + 4 * (count + 1);
  59.     e = elf->e_symtab + elf->e_symlen;
  60.     for (i = 0; i < count; i++, s++) {
  61.         syms[i].as_name = s;
  62.         while (s < e && *s) {
  63.         s++;
  64.         }
  65.         if (s >= e) {
  66.         seterr(ERROR_SIZE_ARSYMTAB);
  67.         free(syms);
  68.         return NULL;
  69.         }
  70.         elf_assert(!*s);
  71.         syms[i].as_hash = elf_hash(syms[i].as_name);
  72.         syms[i].as_off = __load_u32M(elf->e_symtab + 4 * (i + 1));
  73.     }
  74.     syms[count].as_name = NULL;
  75.     syms[count].as_hash = ~0UL;
  76.     syms[count].as_off = 0;
  77.     elf->e_symtab = (char*)syms;
  78.     elf->e_symlen = count + 1;
  79.     elf->e_free_syms = 1;
  80.     }
  81.     *ptr = elf->e_symlen;
  82.     return (Elf_Arsym*)elf->e_symtab;
  83. }
  84.